package com.example.demo;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private ImageView imageView2;
    private ImageView imageView3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imgview);
        imageView2 = findViewById(R.id.imgview2);
        imageView3 = findViewById(R.id.ima3);

        imageView.setImageResource(R.drawable.wifi);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
        animationDrawable.start();
     //   startAlphaAnimation();
     //   startScaleAnimation();
        //startTranslateAnimation();
         // startRotateAnimation();
          startAnimationSet();



    }
    //组合动画
    private void startAnimationSet() {
        AnimationSet animationSet = new AnimationSet(true);
        RotateAnimation rotateAnimation = new RotateAnimation(0,360);
        ScaleAnimation scaleAnimation = new ScaleAnimation
                (2.0f,0.0f,2.0f,0.0f,0.5f,0.5f);
      TranslateAnimation translateAnimation = new TranslateAnimation(0,300,0,300);

        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);

        animationSet.setDuration(3000);
        animationSet.setFillAfter(true);
        animationSet.setRepeatCount(5);

        imageView3.startAnimation(animationSet);


    }

    //平移
    private void startTranslateAnimation() {
        TranslateAnimation translateAnimation = new TranslateAnimation(0,300,0,-100);
        translateAnimation.setDuration(3000);
        translateAnimation.setRepeatCount(3);
        translateAnimation.setRepeatMode(Animation.REVERSE);
        imageView3.startAnimation(translateAnimation);
    }

    //旋转
    private void startRotateAnimation() {
        RotateAnimation rotateAnimation = new RotateAnimation
                (0,360,300,400);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(3);
        imageView3.startAnimation(rotateAnimation);
    }

    //缩放
    private void startScaleAnimation() {
        ScaleAnimation scaleAnimation = new ScaleAnimation
                (2.0f,0.0f,2.0f,0.0f,0.5f,0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatCount(3);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        imageView3.startAnimation(scaleAnimation);
    }

    //透明变化  动画
    private void startAlphaAnimation() {
        //通过xml方式加载动画
        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation
                (MainActivity.this,R.anim.alpha);
        imageView2.startAnimation(alphaAnimation);

        //通过代码实现透明度动画
//        AlphaAnimation alphaAnimation2 = new AlphaAnimation(1.0f,0.0f);
//        alphaAnimation2.setRepeatCount(3);
//        alphaAnimation2.setFillAfter(true);
//        alphaAnimation2.setRepeatMode(Animation.REVERSE);
//        imageView2.startAnimation(alphaAnimation2);
    }
}
